const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
    nav: ["个体车商", "企业车商","4S店认证"],   //获取导航
    ActiveNavItem: 0,
    city: null,   //城市
    tempFilePaths:  [],   //图片
    imgSrc: SERVICE.imgUrl
  },

  /*分享*/
  onShareAppMessage(){

  },
  
  /*导航*/
  getNavItem(e){
    this.setData({
      ActiveNavItem: e.currentTarget.dataset.item
    });
  },

  /*获取城市*/
  getCity(e){
    let city = e.detail.value;
    this.setData({
      city: city[0] + '/' + city[1] + '/' + city[2]
    });
  },

  /*手机号*/
  getMobile(e){
    let mobile = e.detail.value;
    if(mobile.length > 0 && Golbal.checkPhone(mobile) == false){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true,
        duration: 1500
      });
    }
  },

  /*显示选择上传的图片（本地或s使用相机拍照）*/
  UploadingImg(e) {
    let _this = this, td = this.data, 
    key = e.currentTarget.dataset.type,
    index = e.currentTarget.dataset.index;
    wx.chooseImage({
      success: function (res) {
        td[key] = res.tempFiles[0].path;
        for (let i in td.tempFilePaths) {
          if (td.tempFilePaths[i]._key == key) {
            td.tempFilePaths.splice(i, 1);
          }
        }
        td.tempFilePaths.push({ _key: key, path: res.tempFiles[0].path }); 
        _this.setData(td);
      }
    });
  },

  /*清除图片*/
  clearImgItem(e) {
    let _this = this, td = this.data,
    key = e.currentTarget.dataset.type;
    // wx.showModal({
    //   title: "提示",
    //   content: "确定清除该图片吗",
    //   success:function(res){
    //     if(res.confirm){
    //       td[key] = null;
    //       for(let i in td.tempFilePaths){
    //         if (td.tempFilePaths[i]._key == key){
    //           td.tempFilePaths.splice(i, 1); 
    //         }
    //       }  
    //       _this.setData(td);
    //     }
    //   }
    // })

    td[key] = null;
    for (let i in td.tempFilePaths) {
      if (td.tempFilePaths[i]._key == key) {
        wx.showModal({
          title: "提示",
          content: "确定清除该图片吗",
          success: function (res) {
            if (res.confirm) {
              td.tempFilePaths.splice(i, 1);
              _this.setData(td);
            }
          }
        })
      }
    }
    
  },

  /*阅览图片*/
  previewImage(e){
    let imgSrc = e.currentTarget.dataset.src;
    if(imgSrc){
      wx.previewImage({
        urls: [imgSrc],
        success: function(res){
          console.log(res);
        }
      });
    }
  },

  /*上传图片*/
  uploadingImg(i, successUp, failUp, id) {
    wx.showLoading({
      title: "上传图片中...",
      mask: true
    });
    let _this = this, td = this.data;
    if (td.tempFilePaths.length > 0) {
      wx.uploadFile({
        url: "https://www.csdashi.cn/personal/getPersonalData/",
        filePath: td.tempFilePaths[i].path,
        name: "image",
        formData: {
          thirdCookie: wx.getStorageSync("thirdCookie"),
          user_extra_id: id
        },
        success: function (res) {
          console.log("上传成功:", res);
          successUp ++;
        },
        fail(res) {
          console.log("上传失败:", res);
          failUp ++;
        },
        complete() {
          i++;
          if (i < td.tempFilePaths.length) {
            _this.uploadingImg(i, successUp, failUp);
          } else if (i == td.tempFilePaths.length) {
            wx.hideLoading();            
            wx.showToast({
              title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败！',
              icon: "none",
              mask: true,
              duration: 2000,
              success:function(){
                wx.navigateBack();
              }
            });
          }
        }
      });
    }
  }, 
  
  /*提交*/
  formSubmit(e){
    let td = this.data;
    if(td.ActiveNavItem == 0){
      this.test(e);
    } else if (td.ActiveNavItem == 1){
      this.test2(e);
    } else if (td.ActiveNavItem == 2) {
      this.test3(e);
    }
  },

  test(e) {
    
    let form_obj = e.detail.value, td = this.data;

    if (!form_obj.city) {
      wx.showToast({
        title: "请选择所在城市",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.da) {
      wx.showToast({
        title: "请输入详细地址",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.ba) {
      wx.showToast({
        title: "请输入经营地址",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.name) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.bl) {
      wx.showToast({
        title: "请输入身份证号",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.shopname) {
      wx.showToast({
        title: "请输入车行名称",
        icon: "none",
        mask: true,
      })
    } else if (form_obj.mobile.length == 0) {
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true,
      })
    } else if (Golbal.checkPhone(form_obj.mobile) == false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true,
      })
    } else if (!td.ic){
      wx.showToast({
        title: "请上传身份证正面图",
        icon: "none",
        mask: true,
      })
    } else if (!td.ic2) {
      wx.showToast({
        title: "请上传身份证反面图",
        icon: "none",
        mask: true,
      })
    }else if (!form_obj.agreement[0]) {
      wx.showToast({
        title: "未同意车大师用户协议",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.agreement[0]) {
      wx.showToast({
        title: "未同意车大师用户协议",
        icon: "none",
        mask: true,
      })
    }  else {
      this.submit(e);
    }
  },

  test2(e) {
    let form_obj = e.detail.value, td = this.data;

    if (!form_obj.city) {
      wx.showToast({
        title: "请选择所在城市",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.da) {
      wx.showToast({
        title: "请输入详细地址",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.ba) {
      wx.showToast({
        title: "请输入经营地址",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.name) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        mask: true,
      })
    } else if (form_obj.mobile.length == 0) {
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true,
      })
    } else if (Golbal.checkPhone(form_obj.mobile) == false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.shopname) {
      wx.showToast({
        title: "请输入企业名称",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.bl) {
      wx.showToast({
        title: "请输入营业执照注册号",
        icon: "none",
        mask: true,
      })
    } else if (!td.ic) {
      wx.showToast({
        title: "请上传身份证正面图",
        icon: "none",
        mask: true,
      })
    } else if (!td.ic2) {
      wx.showToast({
        title: "请上传身份证反面图",
        icon: "none",
        mask: true,
      })
    } else if (!td.bf){
      wx.showToast({
        title: "请上传营业执照图",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.agreement[0]) {
      wx.showToast({
        title: "未同意车大师用户协议",
        icon: "none",
        mask: true,
      })
    } else {
      this.submit(e);
    }
  },
  
  test3(e){
    let form_obj = e.detail.value, td = this.data;

    if (!form_obj.city) {
      wx.showToast({
        title: "请选择所在城市",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.da) {
      wx.showToast({
        title: "请输入详细地址",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.ba) {
      wx.showToast({
        title: "请输入经营地址",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.name) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        mask: true,
      })
    } else if (form_obj.mobile.length == 0) {
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true,
      })
    } else if (Golbal.checkPhone(form_obj.mobile) == false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.shopname) {
      wx.showToast({
        title: "请输入4s店名称",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.bl) {
      wx.showToast({
        title: "请输入营业执照注册号",
        icon: "none",
        mask: true,
      })
    } else if (!td.ic) {
      wx.showToast({
        title: "请上传身份证正面图",
        icon: "none",
        mask: true,
      })
    } else if (!td.ic2) {
      wx.showToast({
        title: "请上传身份证反面图",
        icon: "none",
        mask: true,
      })
    } else if (!td.bf) {
      wx.showToast({
        title: "请上传营业执照图",
        icon: "none",
        mask: true,
      })
    } else if (!td.css){
      wx.showToast({
        title: "请上4S门店图片",
        icon: "none",
        mask: true,
      })
    } else if (!form_obj.agreement[0]) {
      wx.showToast({
        title: "未同意车大师用户协议",
        icon: "none",
        mask: true,
      })
    }else {
      this.submit(e);
    }
  },
  
  submit(e){
    let form_obj = e.detail.value;
    wx.showLoading({
      title: "提交中...",
      mask: true
    });
    let _this = this,td  = this.data;
    SERVICE.POST(SERVICE.URL.userExtra, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      approve_choice: td.ActiveNavItem + 1,
      city: form_obj.city,
      detailAddress: form_obj.da,
      address: form_obj.ba,
      realName: form_obj.name,
      userTel: form_obj.mobile,
      motors: form_obj.shopname,
      user_mark: form_obj.bl,
    }, (status, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        _this.uploadingImg(0, 0, 0, res.data.user_extra_id);
      } else {
        wx.showToast({
          title: "提交失败",
          icon: "none",
          mask: true
        });
      }
    });
  }
});